package com.xiaodaidai.bllogsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaodaidai.bllogsystem.mapper.CommentMapper;
import com.xiaodaidai.bllogsystem.mapper.StatisticMapper;
import com.xiaodaidai.bllogsystem.po.Comment;
import com.xiaodaidai.bllogsystem.po.Statistic;
import com.xiaodaidai.bllogsystem.service.ICommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
/**
 * ICommentService接口实现类
 * @Date 2021-3-14 13:14
 * @Created by XiaoDaiDai
 */
@Service
@Transactional
public class CommentServiceImpl implements ICommentService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private StatisticMapper statisticMapper;
    // 根据文章id分页查询评论
    public PageInfo<Comment> getComments(Integer aid, int page, int count) {
        PageHelper.startPage(page,count);
        List<Comment> commentList = commentMapper.selectCommentWithPage(aid);
        PageInfo<Comment> pageInfo = new PageInfo<>(commentList);
        return pageInfo;
    }

    // 用户发表评论
    public void pushComment(Comment comment) {
        commentMapper.pushComment(comment);
        // 更新文章评论数据量
        Statistic statistic = statisticMapper.selectStatisticWithArticleId(comment.getArticleId());
        statistic.setCommentsNum(statistic.getCommentsNum()+1);
        statisticMapper.updateArticleCommentsWithId(statistic);
    }
}
